# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymongo
from pymongo import IndexModel, ASCENDING

from dy2018.items import Dy2018Item


class Dy2018Pipeline(object):

    def __init__(self):
        client = pymongo.MongoClient("127.0.0.1", 27017)
        db = client["dy2018"]
        self.dy2018 = db["dy2018"]
        idx = IndexModel([('video_download', ASCENDING)], unique=True)
        self.dy2018.create_indexes([idx])

    def process_item(self, item, spider):
        print('MongoDBItem---$s', item)
        if isinstance(item, Dy2018Item):
            try:
                mongo_find = self.dy2018.find_one(item)
                if mongo_find:
                    self.dy2018.update_one(item)
                else:
                    self.dy2018.insert(item)
            except Exception as e:
                print(e)
        return item
